home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / sleep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  1.1 KB  |  61 lines

  1. #include "lib.h"
  2. /*  sleep(3)
  3.  *
  4.  *  Sleep(n) pauses for 'n' seconds by scheduling an alarm interrupt.
  5.  *
  6.  *  Changed to conform with POSIX      Terrence W. Holm      Oct. 1988
  7.  */
  8.  
  9. #include <signal.h>
  10.  
  11.  
  12. static void  _alfun()    /* Used with sleep() below */
  13.   {
  14.   }
  15.  
  16.  
  17. unsigned sleep( secs )
  18.   unsigned secs;
  19.  
  20.   {
  21.   unsigned current_secs;
  22.   unsigned remaining_secs;
  23. #ifdef __STDC__
  24.   void      (*old_signal)();
  25. #else
  26.   int      (*old_signal)();
  27. #endif
  28.  
  29.   if ( secs == 0 )
  30.     return( 0 );
  31.  
  32.   current_secs = alarm( 0 );   /*  Is there currently an alarm?  */
  33.  
  34.   if ( current_secs == 0  ||  current_secs > secs )
  35.     {
  36.     old_signal = signal( SIGALRM, _alfun );
  37.  
  38.     alarm( secs );
  39.     pause();
  40.     remaining_secs = alarm( 0 );
  41.  
  42.     signal( SIGALRM, old_signal );
  43.  
  44.     if ( current_secs > secs )
  45.     alarm( current_secs - ( secs - remaining_secs ) );
  46.  
  47.     return( remaining_secs );
  48.     }
  49.  
  50.  
  51.   /*  current_secs <= secs,  ie. alarm should occur before secs  */
  52.  
  53.   alarm( current_secs );
  54.   pause();
  55.   remaining_secs = alarm( 0 );
  56.  
  57.   alarm( remaining_secs );
  58.  
  59.   return( secs - ( current_secs - remaining_secs ) );
  60.   }
  61.